package com.hyt.it.ogt.ks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyt.common.utils.exception.BusinessException;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.ks.enums.EsCandidateOperationTrackLogCode;
import com.hyt.it.ogt.ks.es.CandidateOperationTrackLogDTO;
import com.hyt.it.ogt.ks.mapper.CandidateLogAnswerMapper;
import com.hyt.it.ogt.ks.model.entity.CandidateLogAnswer;
import com.hyt.it.ogt.ks.model.vo.CandidateLogBeginTimeVO;
import com.hyt.it.ogt.ks.service.ICandidateLogAnswerService;
import com.hyt.it.ogt.ks.service.es.IEsAndDbService;
import com.hyt.it.ogt.ks.util.LocalDateTimeUtils;
import com.hyt.it.ogt.opertaion.track.model.AnswerLogVO;
import io.seata.common.util.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 轨迹表-答案轨迹表 服务实现类
 * </p>
 *
 * @author liteng
 * @since 2022-11-22
 */
@Service
public class CandidateLogAnswerServiceImpl extends BaseServiceImpl<CandidateLogAnswerMapper, CandidateLogAnswer> implements ICandidateLogAnswerService {

    @Resource
    private IEsAndDbService esAndDbService;

    @Override
    public Boolean saveAnswerLog(List<AnswerLogVO> answerLogVOList) {
        try {
            super.saveBatch(answerLogVOList.stream().map(vo->CandidateLogAnswer.builder()
                    .id(UUIDUtils.randomUUID())
                    .examId(vo.getExamId())
                    .candidateId(vo.getCandidateId())
                    .eventType(vo.getEventType())
                    .operateType(vo.getOperateType())
                    .operateContent(vo.getOperateContent())
                    .itemSortName(vo.getItemSortName())
                    .itemId(vo.getItemId())
                    .itemTypeName(vo.getItemTypeName())
                    .operateUser(vo.getOperateUser())
                    .beginTime(vo.getBeginTime().contains(".") ? LocalDateTime.parse(vo.getBeginTime(), DateTimeFormatter.ofPattern(LocalDateTimeUtils.YYYYMMDDHHMMSSSSS))
                            : LocalDateTime.parse(vo.getBeginTime(), DateTimeFormatter.ofPattern(LocalDateTimeUtils.YYYYMMDDHHMMSS)))
                    .build()).collect(Collectors.toList()));
            //同步ES
            esAndDbService.asyncSaveCandidateOperationTrackLog(answerLogVOList.stream().map(vo -> CandidateOperationTrackLogDTO.builder()
                            .type(EsCandidateOperationTrackLogCode.ANSWER.getCode())
                            .examId(vo.getExamId())
                            .candidateId(vo.getCandidateId())
                            .eventType(vo.getEventType())
                            .operateType(vo.getOperateType())
                            .operateContent(vo.getOperateContent())
                            .operateUser(vo.getOperateUser())
                            .itemSortName(vo.getItemSortName())
                            .itemId(vo.getItemId())
                            .itemTypeName(vo.getItemTypeName())
                            .beginTime(vo.getBeginTime().contains(".") ? LocalDateTime.parse(vo.getBeginTime(), DateTimeFormatter.ofPattern(LocalDateTimeUtils.YYYYMMDDHHMMSSSSS))
                                    : LocalDateTime.parse(vo.getBeginTime(), DateTimeFormatter.ofPattern(LocalDateTimeUtils.YYYYMMDDHHMMSS)))
                            .build()).collect(Collectors.toList()));
        } catch (Exception e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public List<CandidateLogBeginTimeVO> getCandidateLogCommitTime(String examId, String operatorType, String finishType) {
        QueryWrapper<CandidateLogAnswer> query = Wrappers.query();
        query.lambda()
                .select(CandidateLogAnswer::getCandidateId, CandidateLogAnswer::getBeginTime, CandidateLogAnswer::getOperateContent)
                .eq(CandidateLogAnswer::getExamId, examId)
                .eq(CandidateLogAnswer::getOperateType, operatorType);
        List<CandidateLogAnswer> candidateLogAnswerList = super.list(query);
        candidateLogAnswerList = candidateLogAnswerList.stream().filter(c -> c.getOperateContent().contains("\"finishType\":" + finishType)).collect(Collectors.toList());
        List<CandidateLogBeginTimeVO> candidateLogBeginTimeList = new ArrayList<CandidateLogBeginTimeVO>();
        for(CandidateLogAnswer candidateLogAnswer : candidateLogAnswerList) {
            CandidateLogBeginTimeVO candidateLogBeginTime = new CandidateLogBeginTimeVO();
            candidateLogBeginTime.setCandidateId(candidateLogAnswer.getCandidateId());
            candidateLogBeginTime.setBeginTime(candidateLogAnswer.getBeginTime());
            candidateLogBeginTimeList.add(candidateLogBeginTime);
        }
        return candidateLogBeginTimeList;
    }

    @Override
    public void syncAnswerByExamId(String examId) {
        Integer count = baseMapper.getCountByExamId(examId);
        if (count == 0) {
            return;
        }
        long pageTotal;
        long pageSize = 200;
        if (count % pageSize == 0) {
            pageTotal = count / pageSize;
        } else {
            pageTotal = count / pageSize + 1;
        }
        for (int i = 1; i <= pageTotal; i++) {
            long startIndex = (i - 1) * pageSize;
            List<CandidateLogAnswer> answerLogVOList = baseMapper.findListByExamId(examId, startIndex, pageSize);
            if (CollectionUtils.isNotEmpty(answerLogVOList)) {
                esAndDbService.saveCandidateOperationTrackLog(answerLogVOList.stream().map(vo -> CandidateOperationTrackLogDTO.builder()
                        .type(EsCandidateOperationTrackLogCode.ANSWER.getCode())
                        .examId(vo.getExamId())
                        .candidateId(vo.getCandidateId())
                        .eventType(vo.getEventType())
                        .operateType(vo.getOperateType())
                        .operateContent(vo.getOperateContent())
                        .operateUser(vo.getOperateUser())
                        .itemSortName(vo.getItemSortName())
                        .itemId(vo.getItemId())
                        .itemTypeName(vo.getItemTypeName())
                        .takeTime(vo.getTakeTime())
                        .beginTime(vo.getBeginTime())
                        .build()).collect(Collectors.toList()));
            }
        }
    }

    @Override
    public void syncAnswerAll() {
        Integer count = baseMapper.getCountByAll();
        if (count == 0) {
            return;
        }
        long pageTotal;
        long pageSize = 200;
        if (count % pageSize == 0) {
            pageTotal = count / pageSize;
        } else {
            pageTotal = count / pageSize + 1;
        }
        for (int i = 1; i <= pageTotal; i++) {
            long startIndex = (i - 1) * pageSize;
            List<CandidateLogAnswer> answerLogVOList = baseMapper.findListByAll(startIndex, pageSize);
            if (CollectionUtils.isNotEmpty(answerLogVOList)) {
                esAndDbService.saveCandidateOperationTrackLog(answerLogVOList.stream().map(vo -> CandidateOperationTrackLogDTO.builder()
                        .type(EsCandidateOperationTrackLogCode.ANSWER.getCode())
                        .examId(vo.getExamId())
                        .candidateId(vo.getCandidateId())
                        .eventType(vo.getEventType())
                        .operateType(vo.getOperateType())
                        .operateContent(vo.getOperateContent())
                        .operateUser(vo.getOperateUser())
                        .itemSortName(vo.getItemSortName())
                        .itemId(vo.getItemId())
                        .itemTypeName(vo.getItemTypeName())
                        .takeTime(vo.getTakeTime())
                        .beginTime(vo.getBeginTime())
                        .build()).collect(Collectors.toList()));
            }
        }
    }


}
